package com.kmxd.ams.init;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 系统启动后自动执行升级内容
 *
 * @author zuolg
 */
@Component
@Slf4j
public class InitUpgradeCmd implements CommandLineRunner {
    @Value("${app.upgrade}")
    private String upgrade;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private InitUpgradeV3 initUpgradeV3;
    @Autowired
    private InitUpgradeV4 initUpgradeV4;

    @Autowired
    private InitUpgradeV5 initUpgradeV5;
    @Autowired
    private InitUpgradeV6 initUpgradeV6;
    @Override
    public void run(String... args) {
        System.out.println("=========================开始更新 start=================================");
        if (ObjectUtil.equals("v3", upgrade)) {
            initUpgradeV3.initMenu();
        } else if (ObjectUtil.equals("v4", upgrade)) {
            initUpgradeV4.creatLogEventTable();
        } else if (ObjectUtil.equals("v5", upgrade)) {
            initUpgradeV5.resetPassword();
        } else if (ObjectUtil.equals("v6", upgrade)) {
            initUpgradeV6.initDocNumberField();
        } else {
            initUpgradeV4.creatLogEventTable();
        }
        System.out.println("=========================更新结束 end=================================");
        cleanRedis();
        System.out.println("=========================清空redis完成=================================");
        System.out.println("=========================如果没有报错，升级完成=================================");
    }

    private void cleanRedis() {
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            redisTemplate.delete(key);
        }
        System.out.println("清除缓存成功");
    }
}
